
//
//  LoadEditorImageView.m
//  WULIHH
//
//  Created by tang on 16/4/18.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "LoadEditorImageView.h"
#import "Header.h"

#import "IQStickerView.h"

@interface LoadEditorImageView ()<IQStickerViewDelegate>
{
    UIImageView *backImageView;
    IQStickerView *qipaoView;
    
    NSArray *imageNameArray;
    
    NSString *imageString;
    NSDictionary *dic;
    
    NSMutableDictionary *mutableDic;
}

@end




@implementation LoadEditorImageView


- (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
     
        [self setUserInteractionEnabled:YES];

        [self initUI];
        
        imageNameArray = @[@"sixqipao.png"];
    }
    return self;
}



- (void)initUI
{
    backImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    [backImageView setUserInteractionEnabled:YES];
    [self addSubview:backImageView];
}



//加载图片
- (void)loadImage:(UIImage *)image
{
//    NSLog(@"%f", image.size.height/2.0);
    
    [backImageView setImage:image];
    if (WithView != 320 && WithView != 375)
    {
        [backImageView setFrame:CGRectMake(0, 0, image.size.width/3.0, image.size.height/3.0)];
    }
    else
    {
        [backImageView setFrame:CGRectMake(0, 0, image.size.width/2.0, image.size.height/2.0)];
    }
    
//    [self setFrame:backImageView.bounds];
}



- (void)removeQipaoFromView
{
    [qipaoView removeFromSuperview];
}




//设置气泡
- (void)loadQipao:(NSDictionary *)infoDic
{
        float w = 0.0;
        float h = 0.0;

    
    mutableDic = [[NSMutableDictionary alloc] initWithDictionary:infoDic];
    
        imageString = [infoDic valueForKey:@"image"];
        ////    dic = infoDic;
        //
        if ([[infoDic valueForKey:@"image"] isEqualToString:@"noname"])
        {
            //不加载气泡
        }
        else
        {
            w = [[infoDic valueForKey:@"width"] floatValue];
            h = [[infoDic valueForKey:@"height"] floatValue];
            

            
            qipaoView = [[IQStickerView alloc] initWithFrame:CGRectMake([[infoDic valueForKey:@"frameX"] floatValue], [[infoDic valueForKey:@"frameY"] floatValue], w, h)];

            
            qipaoView.rect = CGRectMake([[infoDic valueForKey:@"contX"] floatValue], [[infoDic valueForKey:@"contY"] floatValue], [[infoDic valueForKey:@"lablewidth"] floatValue], [[infoDic valueForKey:@"lableheight"] floatValue]);
            
            
            UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageString]];
            [qipaoView setContentMode:UIViewContentModeScaleAspectFit];
//            [qipaoView setContentView:imageView2];
            [qipaoView setTextString:[infoDic valueForKey:@"twxt"]];
            [qipaoView setTextColor:[self colorWithHexString:[infoDic valueForKey:@"color"] alpha:1.0]];
            [backImageView addSubview:qipaoView];
//            [qipaoView.contentView setAlpha:[[infoDic valueForKey:@"alpha"] floatValue]];
            [qipaoView setTransform:CGAffineTransformMakeRotation([[infoDic valueForKey:@"jiaodu"] doubleValue])];
        }
    
        [qipaoView setDelegate:self];
        //
        //
    [qipaoView hideBiangkuang];
    [_MLoadEditorImageViewDelegate saveImage:[self cropImage] WithqipaoDic:infoDic];
    [qipaoView showBiankuang];

}


- (void)setAlpha:(CGFloat)alpha
{
//    [qipaoView.contentView setAlpha:alpha];
    [mutableDic setValue:[NSString stringWithFormat:@"%f", alpha] forKey:@"alpha"];
//    NSLog(@"%@", mutableDic);
    [qipaoView hideBiangkuang];
    [_MLoadEditorImageViewDelegate saveImage:[self cropImage] WithqipaoDic:mutableDic];
    [qipaoView showBiankuang];
    
}


- (void)setColorTextString:(NSString *)colorTextString
{
    [qipaoView setTextColor:[self colorWithHexString:colorTextString alpha:1.0]];
    [mutableDic setValue:colorTextString forKey:@"color"];
    //    NSLog(@"%@", mutableDic);
    [qipaoView hideBiangkuang];
    [_MLoadEditorImageViewDelegate saveImage:[self cropImage] WithqipaoDic:mutableDic];
    [qipaoView showBiankuang];
}


//裁剪图片
- (UIImage *)cropImage
{
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
    
    [self drawViewHierarchyInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) afterScreenUpdates:YES];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


#pragma mark----保存气泡图片
- (void)saveQipaoBounds:(CGRect)rect withJiaodu:(CGFloat)jiaodu withLableFrameX:(float)lableX withY:(float)lableY withLableW:(float)lablewidth withlableH:(float)lableheight withContent:(NSString *)string withAlpha:(float)alpha
{
    NSDictionary *dicTion = @{@"image":imageString,
                              @"width":[NSString stringWithFormat:@"%f", rect.size.width],
                              @"height":[NSString stringWithFormat:@"%f", rect.size.height],
                              @"jiaodu":[NSString stringWithFormat:@"%f", jiaodu],
                              @"frameX":[NSString stringWithFormat:@"%f", rect.origin.x],
                              @"frameY":[NSString stringWithFormat:@"%f", rect.origin.y],
                              @"contX":[NSString stringWithFormat:@"%f", lableX],
                              @"contY":[NSString stringWithFormat:@"%f", lableY],
                              @"lablewidth":[NSString stringWithFormat:@"%f", lablewidth],
                              @"lableheight":[NSString stringWithFormat:@"%f", lableheight],
                              @"twxt":string,
                              @"alpha":[NSString stringWithFormat:@"%f", alpha]};

    [qipaoView hideBiangkuang];
    [_MLoadEditorImageViewDelegate saveImage:[self cropImage] WithqipaoDic:dicTion];
    [qipaoView showBiankuang];
}

//添加wenzi
- (void)setContentString:(NSString *)contentString
{
    [qipaoView setTextString:contentString];
    [mutableDic setValue:contentString forKey:@"twxt"];
}

//编辑完成文字
- (void)cropIMage
{
    [qipaoView hideBiangkuang];
    [_MLoadEditorImageViewDelegate saveImage:[self cropImage] WithqipaoDic:mutableDic];
    [qipaoView showBiankuang];

}



#pragma mark----删除气泡
- (void)removeQipaoView
{
    [qipaoView removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
